package structure;

public class QueueArray {
    public static void main(String[] args) {
        Queue queue = new Queue(4);
        System.out.println(queue.isEmpty());
        System.out.println(queue.isFull());
        queue.addQueue(777);
        queue.addQueue(2);
        queue.addQueue(3);
      queue.addQueue(4);
        System.out.println(queue.isFull());
        System.out.println(queue.getQueue());
        System.out.println(queue.getQueue());


        queue.info();
        int tem=queue.headQueue();
        System.out.println(tem);


    }
}

class Queue{
    private int maxSize;
    private int front;
    private int rear;
    private int arr[];

    public  Queue(int arrmaxSize){
        maxSize=arrmaxSize;
        arr=new int[maxSize];
        front=-1;
        rear=-1;
    }
    public boolean isFull() {
        return rear+1==maxSize;
    }
    public boolean isEmpty(){
        return front==rear;
    }
    public void addQueue(int data){
        //判断是否满了
        if(isFull()){
            System.out.println("队列已满,不能添加数据");
            return;
        }
        rear++;
        arr[rear]=data;
    }


    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        return arr[++front];
    }

    public void info(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        for(int data:arr){
            System.out.printf("%d ",data);
        }
    }
    //获取队列头部数据
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        return arr[++front];
    }
}


class CircleQueue{
    private int maxsize;
    private int front;
    private int rear;
    private int[] arr;
    public CircleQueue(int arrmaxsize ){
        maxsize=arrmaxsize;
        front=0;
        rear=0;
        arr=new int [maxsize];
    }

    //判空
    public boolean isEmpty(){
        return front==rear;
    }

    //判满
    public boolean isFull(){
        return (rear+1)%maxsize==front;
    }
    //加元素
    public void addQueue(int n){
        if(isFull()){
            System.out.println("队列已满");
            return;
        }
        arr[rear]=n;
        rear=(rear+1)%maxsize;
    }
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("空");
        }
        int ret=arr[front];
        front=(front+1)%maxsize;
        return ret;
    }


}
